package com.zrgj.front.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.sun.xml.internal.ws.api.message.saaj.SaajStaxWriter;
import com.zrgj.common.response.BaseResponse;
import com.zrgj.common.response.StatusCodeEnum;
import com.zrgj.domain.User;
import com.zrgj.domain.UserCollection;
import com.zrgj.manager.service.IUserCollectionService;
import com.zrgj.manager.service.IUserService;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/collection")
public class CollectionController {
    @Reference
    private IUserService userService;
    @Reference
    private IUserCollectionService userCollectionService;

    /**
     * 添加收藏
     * @param userName
     * @param productId
     * @return
     */
    @RequestMapping(value = "/addCollection")
    public BaseResponse addCollection(String userName, Integer productId){
        User user = userService.findByName(userName);
        if (user != null) {
            UserCollection userCollection = new UserCollection();
            userCollection.setUserId(user.getUserId());
            userCollection.setProductId(productId);

            int insert = userCollectionService.insert(userCollection);
            if (insert == -1){
                return new BaseResponse(StatusCodeEnum.REPEAT_TO_ADD,"当前商品已添加到收藏，请勿重复添加");
            }
        }else {
            return new BaseResponse(StatusCodeEnum.NOT_LOGIN);
        }

        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }

    /**
     * 删除收藏
     * @param collectionId
     * @return
     */
    @RequestMapping(value = "/delCollection")
    public BaseResponse delCollection(Integer collectionId){
        userCollectionService.delete(collectionId);
        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }

    /**
     * 获取收藏列表
     * @param userName
     * @return
     */
    @RequestMapping(value = "/getCollectionList")
    public BaseResponse getCollectionList(String userName){
        User user = userService.findByName(userName);
        if (user == null){
            return new BaseResponse(StatusCodeEnum.NOT_LOGIN,"user_loginUI");
        }
        List<UserCollection> select = userCollectionService.select(user.getUserId());
        return new BaseResponse(StatusCodeEnum.SUCCESS,select);
    }


    /**
     * 跳转到收藏
     * @param userName
     * @return
     */
    @RequestMapping(value = "/toCollection")
    public BaseResponse toCollection(String userName){
        User user = userService.findByName(userName);
        if (user == null){
            return new BaseResponse(StatusCodeEnum.NOT_LOGIN,"user_loginUI");
        }
        return new BaseResponse(StatusCodeEnum.SUCCESS,"user_collection");
    }
}
